package com.ruoyi.management.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.car.api.RemoteCarService;
import com.ruoyi.car.api.domain.CheCar;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.management.domain.CheUserCoupon;
import com.ruoyi.user.api.*;
import com.ruoyi.user.api.domain.CheService;
import com.ruoyi.user.api.domain.CheShop;
import com.ruoyi.user.api.domain.CheUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.management.domain.CheOrder;
import com.ruoyi.management.service.ICheOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单管理Controller
 *
 * @author ruoyi
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/management")
public class CheOrderController extends BaseController {
    @Autowired
    private ICheOrderService cheOrderService;
    @Autowired
    private RemoteUsersService remoteUsersService;

    @Autowired
    private RemoteCarService remoteCarService;

    @Autowired
    private RemoteShopService remoteShopService;

    @Autowired
    private RemoteService remoteService;

//    @Autowired
//    private RemoteCouponService remoteCouponService;

    /**
     * 根据用户ID获取订单管理详细信息
     */
//    @RequiresPermissions("management:management:query")
    @RequestMapping("/UserId")
    public AjaxResult getByUserId(Long userId) {
        List<CheOrder> list = cheOrderService.selectCheOrderByUserId(userId);
        for (CheOrder cheOrder1 : list) {
//            Long userId = cheOrder1.getUserId();
//            AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
//            CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
//            cheOrder1.setCheUser(data);

            Long carId1 = cheOrder1.getCarId();
            CheCar byId1 = remoteCarService.getById(carId1, SecurityConstants.INNER);
            cheOrder1.setCheCar(byId1);

            Long shopId2 = cheOrder1.getShopId();
            AjaxResult info1 = remoteShopService.getInfo(shopId2, SecurityConstants.INNER);
            CheShop data2 = JSON.parseObject(JSON.toJSONString(info1.get("data")), CheShop.class);
            cheOrder1.setCheShop(data2);

            Long serviceId3 = cheOrder1.getServiceId();
            AjaxResult info2 = remoteService.getInfo(serviceId3, SecurityConstants.INNER);
            CheService data3 = JSON.parseObject(JSON.toJSONString(info2.get("data")), CheService.class);
            cheOrder1.setCheService(data3);
        }
        return success(list);
    }

    /**
     * 根据核销码获取订单管理详细信息
     */
//    @RequiresPermissions("management:management:query")
    @RequestMapping("/verificationCode")
    public AjaxResult getByVerificationCode(String verificationCode) {
        CheOrder cheOrder1 = cheOrderService.selectCheOrderByVerificationCode(verificationCode);
        Long userId = cheOrder1.getUserId();
        AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
        CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
        cheOrder1.setCheUser(data);
        Long carId = cheOrder1.getCarId();
        CheCar byId = remoteCarService.getById(carId, SecurityConstants.INNER);
        cheOrder1.setCheCar(byId);
        Long shopId = cheOrder1.getShopId();
        AjaxResult info2 = remoteShopService.getInfo(shopId, SecurityConstants.INNER);
        CheShop data2 = JSON.parseObject(JSON.toJSONString(info2.get("data")), CheShop.class);
        cheOrder1.setCheShop(data2);
        Long serviceId = cheOrder1.getServiceId();
        AjaxResult info3 = remoteService.getInfo(serviceId, SecurityConstants.INNER);
        CheService data3 = JSON.parseObject(JSON.toJSONString(info3.get("data")), CheService.class);
        cheOrder1.setCheService(data3);
        return success(cheOrder1);
    }


    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("management:management:list")
    @GetMapping("/list")
    public TableDataInfo list(CheOrder cheOrder) {
        startPage();
        List<CheOrder> list = cheOrderService.selectCheOrderList(cheOrder);
        for (CheOrder cheOrder1 : list) {
            Long userId = cheOrder1.getUserId();
            AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
            CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
            cheOrder1.setCheUser(data);

            Long carId1 = cheOrder1.getCarId();
            CheCar byId1 = remoteCarService.getById(carId1, SecurityConstants.INNER);
            cheOrder1.setCheCar(byId1);

            Long shopId2 = cheOrder1.getShopId();
            AjaxResult info1 = remoteShopService.getInfo(shopId2, SecurityConstants.INNER);
            CheShop data2 = JSON.parseObject(JSON.toJSONString(info1.get("data")), CheShop.class);
            cheOrder1.setCheShop(data2);

            Long serviceId3 = cheOrder1.getServiceId();
            AjaxResult info2 = remoteService.getInfo(serviceId3, SecurityConstants.INNER);
            CheService data3 = JSON.parseObject(JSON.toJSONString(info2.get("data")), CheService.class);
            cheOrder1.setCheService(data3);
//            Long couponId4 = cheOrder1.getCouponId();
//            AjaxResult info3 = remoteCouponService.getInfo(couponId4, SecurityConstants.INNER);
//            cheOrder1.setCheUserCoupon(JSON.parseObject(JSON.toJSONString(info3.get("data")), CheUserCoupon.class));


        }
        return getDataTable(list);
    }

    /*
        根据商户id查询订单
     */
    @RequiresPermissions("management:management:list")
    @GetMapping(value = "/byshopid/{shop_id}")
    public TableDataInfo getListByShopId(@PathVariable("shop_id") Long shop_id) {
        startPage();
        List<CheOrder> list = cheOrderService.getListByShopId(shop_id);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("management:management:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheOrder cheOrder) {
        List<CheOrder> list = cheOrderService.selectCheOrderList(cheOrder);
        ExcelUtil<CheOrder> util = new ExcelUtil<CheOrder>(CheOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @RequiresPermissions("management:management:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CheOrder cheOrder1 = cheOrderService.selectCheOrderById(id);
        Long userId = cheOrder1.getUserId();
        AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
        CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
        cheOrder1.setCheUser(data);
        Long carId = cheOrder1.getCarId();
        CheCar byId = remoteCarService.getById(carId, SecurityConstants.INNER);
        cheOrder1.setCheCar(byId);
        Long shopId = cheOrder1.getShopId();
        AjaxResult info2 = remoteShopService.getInfo(shopId, SecurityConstants.INNER);
        CheShop data2 = JSON.parseObject(JSON.toJSONString(info2.get("data")), CheShop.class);
        cheOrder1.setCheShop(data2);
        Long serviceId = cheOrder1.getServiceId();
        AjaxResult info3 = remoteService.getInfo(serviceId, SecurityConstants.INNER);
        CheService data3 = JSON.parseObject(JSON.toJSONString(info3.get("data")), CheService.class);
        cheOrder1.setCheService(data3);
        return success(cheOrder1);
    }

    /**
     * 新增订单管理
     */
    @RequiresPermissions("management:management:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheOrder cheOrder) {
        return toAjax(cheOrderService.insertCheOrder(cheOrder));
    }

    /**
     * 修改订单状态
     */
//    @RequiresPermissions("management:management:edit")
//    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateOrderStatus/{id}")
    public AjaxResult updateOrderStatus(@PathVariable Long id) {
        return toAjax(cheOrderService.updateOrderStatus(id));
    }

    /**
     * 修改订单管理
     */
    @RequiresPermissions("management:management:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheOrder cheOrder) {
        return toAjax(cheOrderService.updateCheOrder(cheOrder));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("management:management:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cheOrderService.deleteCheOrderByIds(ids));
    }
}
